﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.Index"/>.
    /// </summary>
    public class IndexVM : ArgumentsOperatorVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(Index),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new IndexVM((Index)codeObject, dictionary); });
        }

        #endregion

        #region /* FIELDS */

        protected SymbolicRefVM _indexerRefVM;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.Index"/>.
        /// </summary>
        public IndexVM(Index index, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(index, dictionary)
        {
            _indexerRefVM = (SymbolicRefVM)CreateVM(index.HiddenRef, false, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.Index"/> model.
        /// </summary>
        public Index Index
        {
            get { return (Index)CodeObject; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        protected override void RenderName(CodeRenderer renderer, RenderFlags flags)
        {
            _expressionVM.Render(renderer, flags);
        }

        protected override Brush RenderBrush(RenderFlags flags)
        {
            return (_indexerRefVM != null ? _indexerRefVM.GetMessageBrush(PUNC_BRUSH, flags) : PUNC_BRUSH);
        }

        protected override void RenderStartArguments(CodeRenderer renderer, Brush brush, RenderFlags flags)
        {
            renderer.RenderText(Index.ParseTokenStart, brush, this);
        }

        protected override void RenderEndArguments(CodeRenderer renderer, Brush brush, RenderFlags flags)
        {
            if (Index.IsEndFirstOnLine)
                renderer.NewLine();
            renderer.RenderText(Index.ParseTokenEnd, brush, this);
        }

        public override void UnRender()
        {
            if (_indexerRefVM != null)
                _indexerRefVM.UnRender();
            base.UnRender();
        }

        public override void RenderToolTip(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderToolTip(renderer, flags);
            if (_indexerRefVM != null)
            {
                renderer.NewLine();
                CreateVM(Index.HiddenRef).RenderToolTip(renderer, RenderFlags.SuppressLineCol);
            }
        }

        #endregion
    }
}
